package com.yand.thread.getMoney2;

public class Bank {

    private Account account;


    public Bank(Account account) {
        this.account = account;
    }

    // 柜台Counter取钱的方法
    public void Counter(int money) {// 参数是每次取走的钱
        synchronized (account){
            if(account.getMoney() >= money){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                int moneys = account.getMoney();
                moneys -= money;//取钱后总数减少
                account.setMoney(moneys);
                System.out.println("A取走了" + money + "还剩下" + (account.getMoney()));
            }else{
                System.out.println("A钱不够了...");
            }
        }


    }
    // ATM取钱的方法
    public void ATM(int money) {// 参数是每次取走的钱
        synchronized (account){
            if(account.getMoney() >= money){
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                int moneys = account.getMoney();
                moneys -= money;//取钱后总数减少
                account.setMoney(moneys);
                System.out.println("B取走了" + money + "还剩下" + (account.getMoney()));
            }else{
                System.out.println("B钱不取了...");
            }
        }


    }




}
